<template>
	<view class="flex flex-column vh100 w100">
		<view class="u-flex-1 w100">
			<map style="width: 100%; height: 100%" :scale="12" :latitude="latitude" :longitude="longitude"
				:markers="covers" :polyline="polyline">
				<cover-image v-if="info.status == 1||info.status ==2" class="navigation" @click="toNav(info.status)"
					src="../../static/nav.png"></cover-image>
			</map>
		</view>
		<orderDetail :info="info" :price="price" @update="init"></orderDetail>
	</view>
</template>

<script>
	import amapFile from '../../libs/amap-wx.js'
	export default {
		data() {
			return {
				id: '', //订单id
				info: {},
				price: '', // 实时价格
				timer1: null, //判断用户取消订单
				timer2: null, //实时更新价格
				// 地图
				latitude: '',
				longitude: '',
				polyline: []
			}
		},
		computed: {
			covers() {
				return [{
					id: 1,
					latitude: this.latitude,
					longitude: this.longitude,
					iconPath: '../../static/now.png',
					title: '当前位置',
					width: 25,
					height: 25
				}, {
					id: 2,
					latitude: this.info.start_latitude,
					longitude: this.info.start_longitude,
					iconPath: '../../static/start.png',
					title: '起点位置',
					width: 40,
					height: 40
				}, {
					id: 3,
					latitude: this.info.end_latitude,
					longitude: this.info.end_longitude,
					iconPath: '../../static/end.png',
					title: '终点位置',
					width: 40,
					height: 40
				}]
			}
		},
		onLoad(option) {
			this.id = option.id
		},
		onUnload() {
			clearInterval(this.timer1)
			clearInterval(this.timer2)
		},
		onHide() {
			clearInterval(this.timer1)
			clearInterval(this.timer2)
		},
		onShow() {
			this.init()
		},
		methods: {
			init() {
				let _this = this
				this.$http('/addons/ddrive/order/info', {
					order_id: this.id
				}, "POST").then(data => {
					this.info = data
					if (this.info.status == 1 || this.info.status == 5) {
						this.timer1 = setInterval(() => {
							this.updata()
						}, 5000)
					} else if (this.info.status == 2) {
						this.timer2 = setInterval(() => {
							this.getPrice()
						}, 30000)
					} else if (this.info.status == 3 || this.info.status == 99) {
						this.price = this.info.price
					}
					uni.getLocation({
						type: 'gcj02',
						success: function(res) {
							console.log('当前位置的经度：' + res.longitude);
							console.log('当前位置的纬度：' + res.latitude);
							_this.longitude = res.longitude
							_this.latitude = res.latitude
							_this.polyline = []
							_this.getLine()
						}
					});
				})
			},
			updata() {
				this.$http('/addons/ddrive/order/info', {
					order_id: this.id
				}, "POST").then(data => {
					this.info = data
					if (this.info.status == 3 || this.info.status == 99) {
						this.price = this.info.price
					}
					if (this.info.status != 1) {
						clearInterval(this.timer1)
					}
					if (this.info.status != 2) {
						clearInterval(this.timer2)
					}
				})
			},
			// 行程中更新价格
			getPrice() {
				let _this = this
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success(res) {
						_this.$http('/addons/ddrive/order/updateLocation', {
							order_id: _this.info.id,
							latitude: res.latitude,
							longitude: res.longitude
						}, "POST").then((data) => {
							_this.price = data.price
						})
					}
				});
			},
			getLine() {
				var that = this;
				var key = '5fc2dfb68927e5b95e03e7c0eca7974f';
				var myAmapFun = new amapFile.AMapWX({
					key: key
				});
				// 1 还没接到人  所在地到起点，起点到终点
				let arr = ['0', '1', '5', '4']
				if (arr.includes(this.info.status)) {
					this.lineData(myAmapFun, this.longitude + ',' + this.latitude, this.info.start_longitude + ',' + this
						.info.start_latitude,
						"#ff4b4e")
					this.lineData(myAmapFun, this.info.start_longitude + ',' + this.info.start_latitude, this.info
						.end_longitude + ',' +
						this.info.end_latitude,
						"#28d32e")
				}
				// 2 途中  所在地到终点
				if (this.info.status == 2) {
					this.lineData(myAmapFun, this.info.start_longitude + ',' + this.info.start_latitude, this.longitude +
						',' + this.latitude,
						"#c6c6c5")
					this.lineData(myAmapFun, this.longitude + ',' + this.latitude, this.info.end_longitude + ',' + this
						.info.end_latitude,
						"#28d32e")
				}
				// 3 完成、取消、待支付：起点到终点
				let arr1 = ['-1', '3', '99']
				if (arr1.includes(this.info.status)) {
					this.lineData(myAmapFun, this.info.start_longitude + ',' + this.info.start_latitude, this.info
						.end_longitude + ',' +
						this.info.end_latitude,
						"#28d32e")
				}
			},
			lineData(myAmapFun, origin, destination, color) {
				let that = this
				myAmapFun.getDrivingRoute({
					origin,
					destination,
					success(data) {
						if (data.paths && data.paths[0] && data.paths[0].steps) {
							var steps = data.paths[0].steps;
							var points = [];
							for (var i = 0; i < steps.length; i++) {
								var poLen = steps[i].polyline.split(';');
								for (var j = 0; j < poLen.length; j++) {
									points.push({
										longitude: parseFloat(poLen[j].split(',')[0]),
										latitude: parseFloat(poLen[j].split(',')[1])
									})
								}
							}
						}
						that.polyline.push({
							points: points,
							color,
							width: 10,
							arrowLine: true,
						})
					},
					fail(info) {
						console.log(444444, info);
					}
				});
			},
			toNav(type) {
				if (uni.getSystemInfoSync().platform == 'android') {
					if (type == 1) {
						plus.runtime.openURL("androidamap://navi?sourceApplication=appname&lat=" + this.info
							.start_latitude + "&lon=" + this.info.start_longitude + "&dev=0&style=2")
					} else if (type == 2) {
						plus.runtime.openURL("androidamap://navi?sourceApplication=ddcar&lat=" + this.info.end_latitude +
							"&lon=" + this.info.end_longitude + "&dev=0&style=2")
					}
				} else {
					if (type == 1) {
						plus.runtime.openURL("iosamap://navi?sourceApplication=appname&lat=" + this.info.start_latitude +
							"&lon=" + this.info.start_longitude + "&dev=0&style=2")
					} else if (type == 2) {
						plus.runtime.openURL("iosamap://navi?sourceApplication=ddcar&lat=" + this.info.end_latitude +
							"&lon=" + this.info.end_longitude + "&dev=0&style=2")
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.navigation {
		position: fixed;
		right: 10rpx;
		top: 100rpx;
		width: 100rpx;
		height: 100rpx;
	}
</style>
